package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.Customer;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.BusinessTrackRecordMapper;
import com.qk.mapper.CustomerMapper;
import com.qk.service.BusinessService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public PageResult<Business> listBusiness(BusinessDto businessDto) {
        Page<Business> page = businessMapper.listBusinesses(new Page<Business>(businessDto.getPage(), businessDto.getPageSize()), businessDto);
        return new PageResult<Business>(page.getTotal(), page.getRecords());
    }

    @Override
    public Business getBusinessById(Integer id) {
        return businessMapper.getBusinessById(id);
    }

    @Override
    public void trackBusiness(Business business) {
        //1.根据id更新商机基本信息
        business.setStatus(3);
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);

        //2.添加商机跟进记
/*        private Integer id; // 跟进记录id, 主键
        private Integer businessId; // 商机id，关联商机id
        private Integer userId; // 跟进人id，关联用户id
        private Integer trackStatus; // 跟进状态, 1:接通, 2:拒绝, 3:无人接听
        private String keyItems; // 沟通重点
        private LocalDateTime nextTime; // 下次跟进时间
        private String record; // 沟通纪要
        private LocalDateTime createTime; // 创建时间
        //扩展字段
        @TableField(exist = false)
        private String assignName;//跟进人名称*/
        BusinessTrackRecord businessTrackRecord = new BusinessTrackRecord();
        businessTrackRecord.setBusinessId(business.getId());// 商机id
        businessTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());// 跟进人id
        businessTrackRecord.setTrackStatus(business.getTrackStatus());
        businessTrackRecord.setKeyItems(business.getKeyItems().toString());
        businessTrackRecord.setRecord(business.getRecord());// 沟通纪要
        businessTrackRecord.setNextTime(business.getNextTime());
        businessTrackRecord.setCreateTime(LocalDateTime.now());
        businessTrackRecordMapper.insert(businessTrackRecord);

    }

    /**
     * 添加商机为客户
     * @param id
     */
    @Override
    public void toCustomer(Integer id) {
        //1.根据id查询商机信息
        Business business = businessMapper.getBusinessById(id);
        business.setStatus(5);
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);
        //2.保存客户信息 (根据商机组装客户信息)
        Customer customer = new Customer();
        BeanUtils.copyProperties(business, customer);
        customer.setId(null);
        customer.setBusinessId(id);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        customerMapper.insert(customer);

    }
}
